package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.PremaidFood;
import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.domain.vo.StockOperation;
import com.ruoyi.system.mapper.PremaidFoodMapper;
import com.ruoyi.system.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;

/**
 * 订单管理 Service业务层处理
 * 
 * @author liukangzu
 * @date 2025-04-17
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private PremaidFoodMapper premaidFoodMapper;
    /**
     * 查询订单管理 
     * 
     * @param orderId 订单管理 主键
     * @return 订单管理 
     */
    @Override
    public Orders selectOrdersByOrderId(Integer orderId)
    {
        return ordersMapper.selectOrdersByOrderId(orderId);
    }

    /**
     * 查询订单管理 列表
     * 
     * @param orders 订单管理 
     * @return 订单管理 
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单管理 
     * 
     * @param orders 订单管理 
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单管理 
     * 
     * @param orders 订单管理 
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单管理 
     * 
     * @param orderIds 需要删除的订单管理 主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrderIds(Integer[] orderIds)
    {
        return ordersMapper.deleteOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单管理 信息
     * 
     * @param orderId 订单管理 主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrderId(Integer orderId)
    {
        return ordersMapper.deleteOrdersByOrderId(orderId);
    }

    /**
     * 批量关闭订单管理信息
     *
     * @param ids 订单管理主键
     * @return 结果
     */
    @Override
    public int closeOrderByOrderIds(Integer[] ids) {
        return ordersMapper.closeOrderByOrderIds(ids);
    }

    /**
     * 关闭订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int closeOrderByOrderId(Integer orderId) {
        return ordersMapper.closeOrderByOrderId(orderId);
    }
    /**
     * 配货订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public AjaxResult distributionOrderByOrderIds(Integer orderId) {
        // 1. 查询订单信息
        Orders order = ordersMapper.selectOrdersByOrderId(orderId);
        if (order == null) {
            return AjaxResult.error("订单不存在: " + orderId);
        }

        // 2. 检查订单状态
        if (order.getOrderState() != 0) {
            return AjaxResult.error("订单当前状态不可配货");
        }

        // 3. 按生产日期升序获取库存批次
        List<Stock> stockList = stockMapper.selectByPremaidFoodIdOrderByDate(
                order.getOrderPremaidFoodId());

        int required = order.getOrderCount();
        List<StockOperation> operations = new ArrayList<>();

        // 4. 计算需要从各批次扣除的数量
        for (Stock stock : stockList) {
            if (required <= 0) break;

            int deduct = Math.min(required, stock.getStockCount());
            operations.add(new StockOperation(stock.getStockId(), deduct));
            required -= deduct;
        }

        // 5. 检查库存是否足够
        if (required > 0) {
            PremaidFood food = premaidFoodMapper.selectPremaidFoodByPremaidFoodId(
                    order.getOrderPremaidFoodId());
            return AjaxResult.error("库存不足，预制菜【" + food.getPremaidFoodName() +
                    "】还需要 " + required + " 份");
        }

        // 6. 执行库存扣减
        for (StockOperation op : operations) {
            stockMapper.deductStock(op.getStockId(), op.getDeductAmount());
        }

        // 7. 更新订单状态
        order.setOrderState(1); // 配货成功
        ordersMapper.updateOrders(order);


        return AjaxResult.success("配货成功");
    }

}
